﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace PJAA_Inventory_and_AR___Prototype
{
    public partial class Add_Client : Form
    {
        SQLMaster master;
        private string type;
        private string Client_id;

        public Add_Client(string type,string Client_id)
        {
            InitializeComponent();
            //
            //added this block to make the form transparent
            //
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            this.BackColor = Color.Transparent;

            master = new SQLMaster();
            this.Client_id = Client_id;

            this.type = type;


            //set to invisible all grpbox details 
            #region groupbox visibility start: false
            grpBox_Cmpany.Visible = false;
            GrpBox_Client.Visible = false;
            groupBox_Address.Visible = false;
            groupBox_ContactDetails.Visible = false;
            AddCl_OK_Btn.Visible = false;
            AddCl_Cancel_Btn.Visible = false;

            if (type != "edit")
            {
                radioButton_promoter.Checked = false;
                AddCl_Walk_RadioBtn.Checked = false;
                AddCl_Jes_RadioBtn.Checked = false;
                AddCl_Phone_RadioBtn.Checked = false;
                AddCl_Dist_RadioBtn.Checked = false;
            }

            #endregion
            if (type == "edit")
            {
                loadEditvalues();
                if (Client_id.Contains("CMP"))
                {
                    AddCl_Dist_RadioBtn.Checked = true;
                }
                if (Client_id.Contains("PRM"))
                {
                    radioButton_promoter.Checked = true;
                }
                if (Client_id.Contains("JES"))
                {
                    AddCl_Jes_RadioBtn.Checked = true;
                }
                if (Client_id.Contains("PHN"))
                {
                    AddCl_Phone_RadioBtn.Checked = true;
                }
                if (Client_id.Contains("WLK"))
                {
                    AddCl_Walk_RadioBtn.Checked = true;
                }
            }
        }

        private void loadEditvalues() 
        {
            object[] clientDetails = master.loadAlldetailsClientInfo(this.Client_id);
            
            TxtBox_CompanyName.Text= clientDetails[1].ToString();
            textBox_ClientFirst.Text = clientDetails[2].ToString();
            textBox_ClientLast.Text = clientDetails[3].ToString();
            textBox_PointPersonFirst.Text = clientDetails[4].ToString();
            textBox_pointPersonLast.Text = clientDetails[5].ToString();
            textBox_st.Text = clientDetails[6].ToString();
            textBox_Brgy.Text = clientDetails[7].ToString();
            TxtBox_cty.Text = clientDetails[8].ToString();
            TxtBox_ContactNo.Text = clientDetails[9].ToString();
            textBox_Fax.Text = clientDetails[10].ToString(); 

            
        }


        private void Distributor_Load(object sender, EventArgs e)
        {

        }

        private void button2_Click(object sender, EventArgs e)
        {
            Close();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            
            #region This block is to INSERT data in SQL 
            
            string clientype = "";
            string lastclientid = "";

            #region elseif chain clienttype
            if (AddCl_Dist_RadioBtn.Checked)
            {
                clientype = "CMP";
                if (master.checkifNoDistributorRecordedYet() == true)//cheeck if there is now distributor record yet
                {//if there already is a distributor record
                    lastclientid = master.getLastClientID(clientype).ToString();//get the last client id and convet to string
                }
                else
                {
                    lastclientid = "CMP000001";
                }
            }
            else if (radioButton_promoter.Checked)
            {
                if (master.checkifNoPromoterRecordedYet()== true)//cheeck if there is now distributor record yet
                {//if there already is a distributor record
                    lastclientid = master.getLastClientID(clientype).ToString();//get the last client id and convet to string
                }
                else
                {
                    lastclientid = "PRM000001";
                }
                
                clientype = "PRM";
            }
            else if (AddCl_Jes_RadioBtn.Checked)
            {
                if (master.checkifNoJesuitRecordedYet()== true)//cheeck if there is now distributor record yet
                {//if there already is a distributor record
                    lastclientid = master.getLastClientID(clientype).ToString();//get the last client id and convet to string
                }
                else
                {
                    lastclientid = "JES000001";
                }
                clientype = "JES";
            }
            else if (AddCl_Walk_RadioBtn.Checked)
            {
                if (master.checkifNoWalkinRecordedYet() == true)//cheeck if there is now distributor record yet
                {//if there already is a distributor record
                    lastclientid = master.getLastClientID(clientype).ToString();//get the last client id and convet to string
                }
                else
                {
                    lastclientid = "WLK000001";
                }
                clientype = "WLK";
            }
            else
            {
                if (master.checkifNoPhoneRecordedYet()== true)//cheeck if there is now distributor record yet
                {//if there already is a distributor record
                    lastclientid = master.getLastClientID(clientype).ToString();//get the last client id and convet to string
                }
                else
                {
                    lastclientid = "PHN000001";
                }
                clientype = "PHN";
            }
            #endregion

            
            

            int lastclientid_no_s =Convert.ToInt16(lastclientid.ToString().Substring(3, 6));//get the last client id (number)//
            lastclientid_no_s++;//increment the id number


            if (clientype == "CMP")
            {
                if (TxtBox_CompanyName.Text.Equals("") || TxtBox_CompanyName.Text.Equals(null) ||
                TxtBox_cty.Text.Equals("") || TxtBox_cty.Text.Equals(null) ||
                TxtBox_ContactNo.Text.Equals("") || TxtBox_ContactNo.Text.Equals(null))
                {
                    MessageBox.Show("Please Complete the required fields properly");   
                }

                else
                {
                    if (this.type == "edit")
                    {
                        master.UpdateClientinfo(this.Client_id, TxtBox_CompanyName.Text,
                    textBox_ClientFirst.Text, textBox_ClientLast.Text, textBox_PointPersonFirst.Text, textBox_pointPersonLast.Text,
                    textBox_st.Text, textBox_Brgy.Text, TxtBox_cty.Text, TxtBox_ContactNo.Text, textBox_Fax.Text);

                        MessageBox.Show("Successfully edited" + TxtBox_CompanyName.Text + " " + textBox_ClientFirst.Text + " " + textBox_ClientLast.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();//close the form
                    }
                    else
                    {


                        //add client if data are not blank
                        master.addClient(clientype + lastclientid_no_s.ToString().PadLeft(6, '0'), TxtBox_CompanyName.Text,
                    textBox_ClientFirst.Text, textBox_ClientLast.Text, textBox_PointPersonFirst.Text, textBox_pointPersonLast.Text,
                    textBox_st.Text, textBox_Brgy.Text, TxtBox_cty.Text, TxtBox_ContactNo.Text, textBox_Fax.Text);
                        //message box to show confirmation
                        MessageBox.Show("Successfully added " + TxtBox_CompanyName.Text + " " + textBox_ClientFirst.Text + " " + textBox_ClientLast.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();//close the form
                    }
                }
            }
            else
            {
                if (textBox_ClientFirst.Text.Equals("") || textBox_ClientFirst.Text.Equals(null) ||
                textBox_ClientLast.Text.Equals("") || textBox_ClientLast.Text.Equals(null) ||
                TxtBox_cty.Text.Equals("") || TxtBox_cty.Text.Equals(null) ||
                TxtBox_ContactNo.Text.Equals("") || TxtBox_ContactNo.Text.Equals(null))
                {
                    MessageBox.Show("Please Complete the required fields properly");   
                }
                else
                {
                    if (this.type == "edit")
                    {
                        master.UpdateClientinfo(this.Client_id, TxtBox_CompanyName.Text,
                    textBox_ClientFirst.Text, textBox_ClientLast.Text, textBox_PointPersonFirst.Text, textBox_pointPersonLast.Text,
                    textBox_st.Text, textBox_Brgy.Text, TxtBox_cty.Text, TxtBox_ContactNo.Text, textBox_Fax.Text);
                        //message box to show confirmation
                        MessageBox.Show("Successfully edited " + TxtBox_CompanyName.Text + " " + textBox_ClientFirst.Text + " " + textBox_ClientLast.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();//close the form
                    }
                    else
                    {
                        //add client if data are not blank
                        master.addClient(clientype + lastclientid_no_s.ToString().PadLeft(6, '0'), TxtBox_CompanyName.Text,
                    textBox_ClientFirst.Text, textBox_ClientLast.Text, textBox_PointPersonFirst.Text, textBox_pointPersonLast.Text,
                    textBox_st.Text, textBox_Brgy.Text, TxtBox_cty.Text, TxtBox_ContactNo.Text, textBox_Fax.Text);
                        //message box to show confirmation
                        MessageBox.Show("Successfully added " + TxtBox_CompanyName.Text + " " + textBox_ClientFirst.Text + " " + textBox_ClientLast.Text, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                        this.Close();//close the form
                    }
                }
            }

            #endregion


        }

        private void label1_Click(object sender, EventArgs e)
        {

        }

        private void label2_Click(object sender, EventArgs e)
        {

        }

        private void label3_Click(object sender, EventArgs e)
        {

        }

        private void textBox1_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox2_TextChanged(object sender, EventArgs e)
        {

        }

        private void textBox3_TextChanged(object sender, EventArgs e)
        {

        }

        private void AddCl_Dist_RadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            visibility(true);
        }


        private void visibility(bool visible)
        {
            if (AddCl_Dist_RadioBtn.Checked)
            {
                grpBox_Cmpany.Visible = true;
                GrpBox_Client.Visible = false;
                groupBox_Address.Visible = true;
                groupBox_ContactDetails.Visible = true;
                AddCl_OK_Btn.Visible = true;
                AddCl_Cancel_Btn.Visible = true;
            }

            if (AddCl_Jes_RadioBtn.Checked || AddCl_Phone_RadioBtn.Checked || AddCl_Walk_RadioBtn.Checked || radioButton_promoter.Checked)
            {
                grpBox_Cmpany.Visible = false;
                GrpBox_Client.Visible = true;
                groupBox_Address.Visible = true;
                groupBox_ContactDetails.Visible = true;
                AddCl_OK_Btn.Visible = true;
                AddCl_Cancel_Btn.Visible = true;
            }
        }

        private void groupBox_ContactDetails_Enter(object sender, EventArgs e)
        {

        }

        private void groupBox_Address_Enter(object sender, EventArgs e)
        {

        }

        private void GrpBox_Client_Enter(object sender, EventArgs e)
        {

        }

        private void grpBox_Cmpany_Enter(object sender, EventArgs e)
        {

        }

        private void AddCl_Walk_RadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            visibility(true);
        }

        private void AddCl_Phone_RadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            visibility(true);
        }

        private void AddCl_Jes_RadioBtn_CheckedChanged(object sender, EventArgs e)
        {
            visibility(true);
        }

        private void radioButton_promoter_CheckedChanged(object sender, EventArgs e)
        {
            visibility(true);
        }
    }
}
